import {   RESPONSE_CODE, STORE_KEY } from "@/common/config";
import { handleInitStoreBaseInfo, showToast } from "@/common/util";
import PendingPop from "@/components/pendingPop/pendingPop";
import useProfileInfoAction from "@/hooks/useProfileInfoAction";
import { IUserProfileEditPromise, USER_PROFILE_STATUS } from "@/typing";
import { View, Textarea } from "@tarojs/components";
import Taro, { useDidShow } from "@tarojs/taro";
import { Suspense, useState } from "react";


const EditNickName: React.FC = () => {

    const [userDescription, setUserDescription] = useState('')
    const [userDescriptionT, setUserDescriptionT] = useState('')
    const { handleShopUserprofileEdit } = useProfileInfoAction()

    const [pendingPopShow, setPendingPopShow] = useState(false)

    const handleInitDesc = () => {
        const baseUserInfo = handleInitStoreBaseInfo();
        if (baseUserInfo) {
            const introduction = baseUserInfo['introduction']
            setUserDescription(introduction)
            setUserDescriptionT(introduction)

        }

    }
    const handleUserDescriptionInput = (e) => {
        setUserDescription(e.detail.value)


    }
    const handleSaveDescription = () => {
        if (userDescriptionT.length === userDescription.length && userDescriptionT === userDescription) {
            Taro.navigateBack()
        } else {
            Taro.showModal({
                title: '提示',
                content: '确定修改简介吗？',
                success: async function (res) {
                    if (res.confirm) {
                        Taro.showLoading({
                            title: '修改中',
                            mask: true
                        })

                        const editRes = await handleShopUserprofileEdit({ introduction: userDescription }) as IUserProfileEditPromise
                        Taro.hideLoading({

                        })
                        const {code, msg,data } = editRes
                        if (code === RESPONSE_CODE.SUCCESS) {
                            const storeBaseUserInfo = handleInitStoreBaseInfo()
                            storeBaseUserInfo['modify_field']['introduction'] = USER_PROFILE_STATUS.disable
                            Taro.setStorageSync(STORE_KEY.BASE_USER_INFO, JSON.stringify(storeBaseUserInfo))
                            showToast(msg)
                            if(data.status ===USER_PROFILE_STATUS.pending ){
                                setPendingPopShow(true)
                            }else{
                                storeBaseUserInfo['introduction'] = userDescription
                                Taro.setStorageSync(STORE_KEY.BASE_USER_INFO, JSON.stringify(storeBaseUserInfo))
                                Taro.navigateBack()
                            }
                           
            
            
            
                        } else {
                            showToast(msg)
                        }
                        
                        

                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        }
    }
    const handleCallBack = () => {
        setPendingPopShow(false)
        Taro.navigateBack()

    }
    useDidShow(() => {
        handleInitDesc()
    })
    return <>
        <View className="bg-#F6F6F6 min-h-100vh w-full pt-84px px-38px box-border">
            <View className="bg-#fff w-full px-34px box-border py-36px">
                <Textarea className="w-full text-#333333 text-32px h-108px leading-50px " onInput={handleUserDescriptionInput} autoHeight value={userDescription}></Textarea>
            </View>
            <View className="w-464px h-92px flex mx-auto mt-68px items-center justify-center bg-#8232E8 text-#fff font-medium text-30px" onClick={handleSaveDescription}>保存</View>
        </View>
        <Suspense>
            <PendingPop show={pendingPopShow} disableText='简介' callBack={handleCallBack}></PendingPop>
        </Suspense>
    </>
}
export default EditNickName;